package ia04.agent.weather;

import java.io.IOException;
import java.util.Date;

import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;

public class BehavMessWeather extends CyclicBehaviour {

	private int step;
	private AID lastSender;

	public BehavMessWeather()
	{
		step = 0;
		lastSender = null;
	}

	@Override
	public void action()
	{
		switch(step)
		{
			case 0:
				// Check for new messages received
				ACLMessage m = myAgent.receive();
				if(m != null && m.getPerformative() == ACLMessage.REQUEST)
				{
					lastSender = m.getSender();
					((AgentWeather)myAgent).addSubscriber(lastSender);
					++step;
				}
				else
					block();
				break;
			case 1:
				// Build answer
				String answer = ((AgentWeather)myAgent).getWeather();
				
				// Build message
				ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
				msg.setContent(answer);
				msg.addReceiver(lastSender);
				
				// Send message
				myAgent.send(msg);

				// Return to first state, waiting for messages
				step = 0;	
				break;
		}
	}


}
